package com.dianning.service.imp;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.commons.logging.*;

import com.dianning.bean.AdSpace;
import com.dianning.bean.ChannelContact;
import com.dianning.bean.Profile;
import com.dianning.bean.ProfileInfo;
import com.dianning.bean.ProfileLink;
import com.dianning.bean.Visitor;
import com.dianning.dao.AdAdsDAO;
import com.dianning.dao.AdSpaceDAO;
import com.dianning.dao.ChannelContactDAO;
import com.dianning.dao.ChannelDAO;
import com.dianning.dao.ProfileDAO;
import com.dianning.dao.ProfileInfoDAO;
import com.dianning.dao.ProfileLinkDAO;
import com.dianning.dao.VisitorDAO;
import com.dianning.exception.DianningException;
import com.dianning.service.AdService;
import com.dianning.service.ProfileService;
import com.dianning.service.VisitorService;

public class ProfileServiceImp implements ProfileService {

	private static final Log logger = LogFactory.getLog(ProfileServiceImp.class);

	private ProfileDAO profileDAO;
	private ProfileInfoDAO profileInfoDAO;
	private ProfileLinkDAO profileLinkDAO;
	private ChannelDAO channelDAO;
	private ChannelContactDAO channelContactDAO;
	
	public ProfileDAO getProfileDAO() {
		return profileDAO;
	}

	public void setProfileDAO(ProfileDAO profileDAO) {
		this.profileDAO = profileDAO;
	}

	public ProfileInfoDAO getProfileInfoDAO() {
		return profileInfoDAO;
	}

	public void setProfileInfoDAO(ProfileInfoDAO profileInfoDAO) {
		this.profileInfoDAO = profileInfoDAO;
	}

	public ProfileLinkDAO getProfileLinkDAO() {
		return profileLinkDAO;
	}

	public void setProfileLinkDAO(ProfileLinkDAO profileLinkDAO) {
		this.profileLinkDAO = profileLinkDAO;
	}
	
	public ChannelDAO getChannelDAO() {
		return channelDAO;
	}

	public void setChannelDAO(ChannelDAO channelDAO) {
		this.channelDAO = channelDAO;
	}

	public ChannelContactDAO getChannelContactDAO() {
		return channelContactDAO;
	}

	public void setChannelContactDAO(ChannelContactDAO channelContactDAO) {
		this.channelContactDAO = channelContactDAO;
	}

	/**
	 * 保存Profile对象
	 * 
	 * @param profile
	 * 			Profile对象
	 * @return Profile
	 * 			Profile对象
	 */
	public Profile updateProfile(Profile profile){
		return this.getProfileDAO().updateProfile(profile);
	}
	
	/**
	 * 根据主键ID获取Profile对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return Profile
	 * 			Profile对象
	 */
	public Profile findProfileByUid(long uid){
		Profile p = this.getProfileDAO().findProfileByUid(uid);
		if(p.getCcids() == null || p.getCcids().isEmpty()){
			return p;
		}else{
			List clist = new ArrayList();
			for(int i=0;i<p.getCcids().size();i++){
				long ccid =Long.parseLong((String) p.getCcids().get(i));
				ChannelContact cc = this.getChannelContactDAO().findChannelContactById(ccid);
				cc.setChannel(this.getChannelDAO().findChannelById(cc.getChannelId()));
				clist.add(cc);
			}
			p.setCcidList(clist);
			return p;
		}
		
	}
	
	/**
	 * 根据webkey 获取Profile对象
	 * 
	 * @param webkey
	 * 			String
	 * @return Profile
	 * 			Profile对象
	 */
	public Profile findProfileByWebkey(String webkey){
		Profile p = this.getProfileDAO().findProfileByWebkey(webkey);
		if(p == null){
			return null;
		}
		if(p.getCcids() == null || p.getCcids().isEmpty()){
			return p;
		}else{
			List clist = new ArrayList();
			for(int i=0;i<p.getCcids().size();i++){
				long ccid =Long.parseLong((String) p.getCcids().get(i));
				ChannelContact cc = this.getChannelContactDAO().findChannelContactById(ccid);
				cc.setChannel(this.getChannelDAO().findChannelById(cc.getChannelId()));
				clist.add(cc);
			}
			p.setCcidList(clist);
			return p;
		}
	}
	
	/**
	 * 保存ProfileInfo对象
	 * 
	 * @param profileInfo
	 * 			ProfileInfo对象
	 * @return ProfileInfo
	 * 			ProfileInfo对象
	 */
	public ProfileInfo saveProfileInfo(ProfileInfo profileInfo){
		return this.getProfileInfoDAO().saveProfileInfo(profileInfo);
	}
	
	/**
	 * 保存ProfileInfo对象
	 * 
	 * @param profileInfo
	 * 			ProfileInfo对象
	 * @return ProfileInfo
	 * 			ProfileInfo对象
	 */
	public ProfileInfo updateProfileInfo(ProfileInfo profileInfo){
		return this.getProfileInfoDAO().updateProfileInfo(profileInfo);
	}
	
	/**
	 * 删除ProfileInfo对象
	 */
	public void removeProfileInfo(ProfileInfo profileInfo){
		this.getProfileInfoDAO().removeProfileInfo(profileInfo);
	}
	
	/**
	 * 根据主键ID获取ProfileInfo对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return ProfileInfo
	 * 			ProfileInfo对象
	 */
	public ProfileInfo findProfileInfoById(long id){
		return this.getProfileInfoDAO().findProfileInfoById(id);
	}
	
	/**
	 * 获取ProfileInfo个数
	 * 
	 * @return int
	 * 			ProfileInfo个数
	 */
	public long getProfileInfoNum(long uid,String type){
		return this.getProfileInfoDAO().getProfileInfoNum(uid,type);
	}
	
	/**
	 * 获取ProfileInfo列表
	 * 
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param firstResult
	 *            int
	 * @param maxResults
	 *            int
	 * @return List
	 * 			ProfileInfo对象列表
	 */
	public List findProfileInfos(long uid,String type,String orderby,int ascOrDesc){
		return this.getProfileInfoDAO().findProfileInfos(uid, type, orderby, ascOrDesc);
	}
	
	/**
	 * 保存ProfileLink对象
	 * 
	 * @param ProfileLink
	 * 			ProfileLink对象
	 * @return ProfileLink
	 * 			ProfileLink对象
	 */
	public ProfileLink saveProfileLink(ProfileLink profileLink){
		return this.getProfileLinkDAO().saveProfileLink(profileLink);
	}
	
	/**
	 * 保存ProfileLink对象
	 * 
	 * @param ProfileLink
	 * 			ProfileLink对象
	 * @return ProfileLink
	 * 			ProfileLink对象
	 */
	public ProfileLink updateProfileLink(ProfileLink profileLink){
		return this.getProfileLinkDAO().updateProfileLink(profileLink);
	}
	
	/**
	 * 删除ProfileLink对象
	 */
	public void removeProfileLink(ProfileLink profileLink){
		this.getProfileLinkDAO().removeProfileLink(profileLink);
	}
	
	/**
	 * 根据主键ID获取ProfileLink对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return ProfileLink
	 * 			ProfileLink对象
	 */
	public ProfileLink findProfileLinkById(long id){
		return this.getProfileLinkDAO().findProfileLinkById(id);
	}
	
	/**
	 * 获取ProfileLink个数
	 * 
	 * @return int
	 * 			ProfileLink个数
	 */
	public long getProfileLinkNum(long uid){
		return this.getProfileLinkDAO().getProfileLinkNum(uid);
	}
	
	/**
	 * 获取ProfileLink列表
	 * 
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param firstResult
	 *            int
	 * @param maxResults
	 *            int
	 * @return List
	 * 			ProfileLink对象列表
	 */
	public List findProfileLinks(long uid,String orderby, int ascOrDesc){
		return this.getProfileLinkDAO().findProfileLinks(uid, orderby, ascOrDesc);
	}
}
